from django.db import models



# Create your models here.

from login.models import User

#所属行业
class Industry(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="行业名称")
    # 指向自己，实现上下级关系，用于构建多级分类系统
    parent = models.ForeignKey("self",null=True, blank=True, on_delete=models.SET_NULL, related_name="children" ,verbose_name="上级行业")
    order = models.IntegerField(default=0, verbose_name="排序")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")

    empty1 = models.CharField(max_length=100, verbose_name="空字段1", null=True, blank=True)
    empty2 = models.CharField(max_length=100, verbose_name="空字段2", null=True, blank=True)
    empty3 = models.CharField(max_length=100, verbose_name="空字段3", null=True, blank=True)

    class Meta:
        verbose_name = "行业分类"         # 在后台显示为“行业分类”（单数）
        verbose_name_plural = "行业分类"  # 所有记录也叫“行业分类”（中文不分单复数）
        ordering = ['order', 'name']    # 默认先按 order 排，再按 name 排

    def __str__(self):
        return self.name                # 打印对象时，直接显示行业名称


# 职位类别
class JobCategory(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="职位类别")
    # 指向自己，实现上下级关系，用于构建多级分类系统
    parent = models.ForeignKey("self",null=True, blank=True, on_delete=models.SET_NULL, related_name="children" ,verbose_name="上级类别")
    order = models.IntegerField(default=0, verbose_name="排序")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")

    empty1 = models.CharField(max_length=100, verbose_name="空字段1", null=True, blank=True)
    empty2 = models.CharField(max_length=100, verbose_name="空字段2", null=True, blank=True)
    empty3 = models.CharField(max_length=100, verbose_name="空字段3", null=True, blank=True)

    # 配置模型的元信息
    class Meta:
        verbose_name = "职位类别"
        verbose_name_plural = "职位类别"
        ordering = ['order', 'name']

    def __str__(self):
        return self.name


#职位名称
class JobTitle(models.Model):
    name = models.CharField(max_length=100,verbose_name="职位名称")
    # 指向自己，实现上下级关系，用于构建多级分类系统
    parent = models.ForeignKey("self",null=True, blank=True, on_delete=models.SET_NULL, related_name="children" ,verbose_name="上级职位")
    category = models.ForeignKey(JobCategory, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="职位类别")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    order = models.IntegerField(default=0, verbose_name="排序")

    empty1 = models.CharField(max_length=100, verbose_name="空字段1", null=True, blank=True)
    empty2 = models.CharField(max_length=100, verbose_name="空字段2", null=True, blank=True)
    empty3 = models.CharField(max_length=100, verbose_name="空字段3", null=True, blank=True)

    class Meta:
        verbose_name = "职位名称"
        verbose_name_plural = "职位名称"
        ordering = ['category__name', 'order' ,'name']
        unique_together = ('name', 'parent')

    def __str__(self):
        return self.name


# 工作经历
class WorkExperience(models.Model):
    company_name = models.CharField(max_length=100, verbose_name="公司名称")
    industry = models.ForeignKey(Industry, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="所属行业")
    department = models.CharField(max_length=100, verbose_name="所属部门")
    position = models.ForeignKey(JobTitle, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="职位名称")
    start_date = models.DateField(verbose_name="开始时间")
    end_date = models.DateField(null=True, blank=True, verbose_name="结束时间")
    content = models.TextField(max_length=3000, verbose_name="工作内容")
    achievements = models.TextField(max_length=1000, null=True, blank=True, verbose_name="工作业绩")
    # 存储任意合法的 JSON 数据如对象 、数组 、字符串
    skills = models.JSONField(null=True, blank=True, verbose_name="拥有技能")
    is_internship = models.BooleanField(default=False, verbose_name="是否为实习经历")
    hide_from_company = models.BooleanField(default=False, verbose_name="对该公司隐藏简历")

    user = models.ForeignKey('login.User', on_delete=models.CASCADE, verbose_name="用户")

    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    empty1 = models.CharField(max_length=100,verbose_name="空字段1" ,null=True, blank=True)
    empty2 = models.CharField(max_length=100,verbose_name="空字段2" ,null=True, blank=True)
    empty3 = models.CharField(max_length=100,verbose_name="空字段3" ,null=True, blank=True)

    class Meta:
        verbose_name = "工作经历"
        verbose_name_plural = "工作经历"
        ordering = ['-start_date']


#项目经历
class ProjectExperience(models.Model):
    project_name = models.CharField(max_length=100, verbose_name="项目名称")
    #项目角色
    project_role = models.CharField(max_length=100, verbose_name="项目角色")
    #项目连接，用于存储和验证 URL 地址的字段类型，确保用户输入的是一个合法的网址。
    project_link = models.URLField(blank=True, null=True, verbose_name="项目连接")
    #开始时间
    start_date = models.DateField(verbose_name="开始时间")
    #结束时间
    end_date = models.DateField(null=True, blank=True, verbose_name="结束时间")
    #项目描述
    project_description = models.TextField(max_length=3000, verbose_name="项目描述")
    #项目业绩
    project_achievements = models.TextField(max_length=1000, null=True, blank=True, verbose_name="项目业绩")
    #登录用户
    user = models.ForeignKey('login.User', on_delete=models.CASCADE, verbose_name="用户")

    class Meta:
        verbose_name = "项目经历"
        verbose_name_plural = "项目经历"
        ordering = ['-start_date']  # 按开始时间倒序排列

    def __str__(self):
        return f"{self.project_name}-{self.project_role}"
